//app.js
App({
  data: {
    max_page: 10,       // 最多可以返回的页面
  },
  
  openPage: function (url, close) {
    if (!url) return;
    url = `pages/${url}`;
    var pages = getCurrentPages(),  //页面栈
      len = pages.length,
      dlt = '',
      max_page = this.data.max_page,
      target = '/' + url.replace(/^\//, '');

    if (close) return wx.redirectTo({
      url: target,
    })

    for (var i = 0; i < len; i++) {
      var zhan_url = "/" + pages[i].route;
      if (zhan_url.indexOf(target.split('?')[0]) != -1) {
        dlt = i + 1;
        break;
      }
    }

    //页面不在栈中
    if (!dlt) {
      if (len < max_page) {
        wx.navigateTo({
          url: target
        });
      } else {
        wx.redirectTo({
          url: target
        });
      }
    } else {
      wx.navigateBack({
        delta: len - dlt
      });
    }
  },
})